﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

/*
 * Image found at:
 * Jokers Smile. Digital image. Wallpapers Well. Wallpapers Well, 23 Sept. 2013. 
 * Web. 11 Nov. 2014. <http://wallpaperswell.com/jokers-smile-the-dark-knight-movie-wallpaper-hd/>.
 * */

namespace CSE360_SudokuProject
{
    public partial class HighScoreMenu : Form
    {
        //variable to keep track of parent
        MainMenu theMainMenu;

        //constructor
        public HighScoreMenu(MainMenu themainmenu)
        {
            InitializeComponent();
            theMainMenu = themainmenu;
        }

        //return button to close form
        private void ReturnButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //closed event, tells main menu that there is not any highscore forms open
        private void HighScoreMenuFormClosed(object sender, FormClosedEventArgs e)
        {
            theMainMenu.highScoreOpened = false;
        }

        //load event (calls after constructor) calls write score method
        private void HighScoreMenu_Load(object sender, EventArgs e)
        {
            WriteScores();//fix?: doesn't keep alignment
        }

        private void WriteScores() //writes the highscores to the text boxes
        {
            string easyHighscoretext = "";
            string mediumHighscoretext = "";
            string hardHighscoretext = "";
            string hellHighscoretext = "";

            //for loop to attach 10 scores,names,and times of each difficulty to a respective string
            for (int i = 0; i < 10; i++)
            {
                easyHighscoretext += FileManager.highScoreEasyNames[i].PadRight
                    (29 - FileManager.highScoreEasyNames[i].Length) +
                    FileManager.highScoreEasyScores[i].ToString().PadRight
                    (26 - FileManager.highScoreEasyScores[i].ToString().Length)
                    + FileManager.highScoreEasyTimes[i] + "\n";

                mediumHighscoretext += FileManager.highScoreMediumNames[i].PadRight
                    (29 - FileManager.highScoreMediumNames[i].Length) +
                    FileManager.highScoreMediumScores[i].ToString().PadRight
                    (26 - FileManager.highScoreMediumScores[i].ToString().Length)
                    + FileManager.highScoreMediumTimes[i] + "\n";

                hardHighscoretext += FileManager.highScoreHardNames[i].PadRight
                    (29 - FileManager.highScoreHardNames[i].Length) +
                    FileManager.highScoreHardScores[i].ToString().PadRight
                    (26 - FileManager.highScoreHardScores[i].ToString().Length)
                    + FileManager.highScoreHardTimes[i] + "\n";

                hellHighscoretext += FileManager.highScoreHellNames[i].PadRight
                    (29 - FileManager.highScoreHellNames[i].Length) +
                    FileManager.highScoreHellScores[i].ToString().PadRight
                    (26 - FileManager.highScoreHellScores[i].ToString().Length)
                    + FileManager.highScoreHellTimes[i] + "\n";
            }

            //makes the text of texbox the string value
            HighScoreEasyRichTextBox.Text = easyHighscoretext;
            HighScoreMediumRichTextBox.Text = mediumHighscoretext;
            HighScoreHardRichTextBox.Text = hardHighscoretext;
            HighScoreHellRichTextBox.Text = hellHighscoretext;
        }

        public void SaveNewHighScores()
        {
            #region difficulty highscore setting
            //1 = easy, 2 = medium, 3 = hard, 4 = hell
            if (FileManager.difficulty == 1)
            {
                //calculates bonus score off set number minus time
                FileManager.currentScore += 300 - Math.Min(300, (FileManager.currentTimeMinute * 60 + FileManager.currentTimeSecond));
                //shows score in message box (so player knows score for current play)
                MessageBox.Show("Your Score: " + FileManager.currentScore.ToString()
                + "\nYour Time: " + string.Format("{0:00}:{1:00}", FileManager.currentTimeMinute, FileManager.currentTimeSecond));
                //loop checks if new score is higher then any other in top 10
                for (int i = 0; i < 10; i++)
                {
                    //sets j to end of top 10
                    int j = 9;
                    //checks if current is higher then the highscore at pos i
                    if (FileManager.currentScore > FileManager.highScoreEasyScores[i])
                    {
                        //moves all highscores down until j reaches i (the location where currentscore is higher)
                        while (j > i)
                        {
                            FileManager.highScoreEasyScores[j] = FileManager.highScoreEasyScores[j - 1];
                            FileManager.highScoreEasyNames[j] = FileManager.highScoreEasyNames[j - 1];
                            FileManager.highScoreEasyTimes[j] = FileManager.highScoreEasyTimes[j - 1];
                            j--;
                        }
                        //writes current score data at i
                        FileManager.highScoreEasyScores[i] = FileManager.currentScore;
                        FileManager.highScoreEasyNames[i] = FileManager.userName;
                        FileManager.highScoreEasyTimes[i] = FileManager.currentTimeMinute.ToString() + ":" + FileManager.currentTimeSecond.ToString();
                        break;
                    }
                }
            }
            else if (FileManager.difficulty == 2)
            {
                //calculates bonus score off set number minus time
                FileManager.currentScore += 420 - Math.Min(420, (FileManager.currentTimeMinute * 60 + FileManager.currentTimeSecond));
                //shows score in message box (so player knows score for current play)
                MessageBox.Show("Your Score: " + FileManager.currentScore.ToString()
                + "\nYour Time: " + string.Format("{0:00}:{1:00}", FileManager.currentTimeMinute, FileManager.currentTimeSecond));
                //loop checks if new score is higher then any other in top 10
                for (int i = 0; i < 10; i++)
                {
                    //sets j to end of top 10
                    int j = 9;
                    //checks if current is higher then the highscore at pos i
                    if (FileManager.currentScore > FileManager.highScoreMediumScores[i])
                    {
                        //moves all highscores down until j reaches i (the location where currentscore is higher)
                        while (j > i)
                        {
                            FileManager.highScoreMediumScores[j] = FileManager.highScoreMediumScores[j - 1];
                            FileManager.highScoreMediumNames[j] = FileManager.highScoreMediumNames[j - 1];
                            FileManager.highScoreMediumTimes[j] = FileManager.highScoreMediumTimes[j - 1];
                            j--;
                        }
                        //writes current score data at i
                        FileManager.highScoreMediumScores[i] = FileManager.currentScore;
                        FileManager.highScoreMediumNames[i] = FileManager.userName;
                        FileManager.highScoreMediumTimes[i] = FileManager.currentTimeMinute.ToString() + ":" + FileManager.currentTimeSecond.ToString();
                        break;
                    }
                }
            }
            else if (FileManager.difficulty == 3)
            {
                //calculates bonus score off set number minus time
                FileManager.currentScore += 600 - Math.Min(600, (FileManager.currentTimeMinute * 60 + FileManager.currentTimeSecond));
                //shows score in message box (so player knows score for current play)
                MessageBox.Show("Your Score: " + FileManager.currentScore.ToString()
                + "\nYour Time: " + string.Format("{0:00}:{1:00}", FileManager.currentTimeMinute, FileManager.currentTimeSecond));
                //loop checks if new score is higher then any other in top 10
                for (int i = 0; i < 10; i++)
                {
                    //sets j to end of top 10
                    int j = 9;
                    //checks if current is higher then the highscore at pos i
                    if (FileManager.currentScore > FileManager.highScoreHardScores[i])
                    {
                        //moves all highscores down until j reaches i (the location where currentscore is higher)
                        while (j > i)
                        {
                            FileManager.highScoreHardScores[j] = FileManager.highScoreHardScores[j - 1];
                            FileManager.highScoreHardNames[j] = FileManager.highScoreHardNames[j - 1];
                            FileManager.highScoreHardTimes[j] = FileManager.highScoreHardTimes[j - 1];
                            j--;
                        }
                        //writes current score data at i
                        FileManager.highScoreHardScores[i] = FileManager.currentScore;
                        FileManager.highScoreHardNames[i] = FileManager.userName;
                        FileManager.highScoreHardTimes[i] = FileManager.currentTimeMinute.ToString() + ":" + FileManager.currentTimeSecond.ToString();
                        break;
                    }
                }
            }
            else
            {
                //calculates bonus score off set number minus time
                FileManager.currentScore += 1200 - Math.Min(1200, (FileManager.currentTimeMinute * 60 + FileManager.currentTimeSecond));
                //shows score in message box (so player knows score for current play)
                MessageBox.Show("Your Score: " + FileManager.currentScore.ToString()
                + "\nYour Time: " + string.Format("{0:00}:{1:00}", FileManager.currentTimeMinute, FileManager.currentTimeSecond));
                //loop checks if new score is higher then any other in top 10
                for (int i = 0; i < 10; i++)
                {
                    //sets j to end of top 10
                    int j = 9;
                    //checks if current is higher then the highscore at pos i
                    if (FileManager.currentScore > FileManager.highScoreHellScores[i])
                    {
                        //moves all highscores down until j reaches i (the location where currentscore is higher)
                        while (j > i)
                        {
                            FileManager.highScoreHellScores[j] = FileManager.highScoreHellScores[j - 1];
                            FileManager.highScoreHellNames[j] = FileManager.highScoreHellNames[j - 1];
                            FileManager.highScoreHellTimes[j] = FileManager.highScoreHellTimes[j - 1];
                            j--;
                        }
                        //writes current score data at i
                        FileManager.highScoreHellScores[i] = FileManager.currentScore;
                        FileManager.highScoreHellNames[i] = FileManager.userName;
                        FileManager.highScoreHellTimes[i] = FileManager.currentTimeMinute.ToString() + ":" + FileManager.currentTimeSecond.ToString();
                        break;
                    }
                }
            }
            //resets values to 0 for next game
            FileManager.currentScore = 0;
            FileManager.currentTimeMinute = 0;
            FileManager.currentTimeSecond = 0;
            #endregion

            //saves new highscore data
            FileManager.SaveData();
        }
    }
}
